#include "stm32f10x.h"
#include "picture.h"
#include "epd.h"
#include "delay.h"
#include "key.h"
#include "menu.h"
#include <string.h>
#include "usart.h"
#include "oled.h"

uint8_t keynum = 0;
uint8_t keyFlag = 0;
uint8_t menu_index = 0;
uint8_t menu_wallpaper_index = 0;
extern uint8_t internet_flag;

extern char RxPacket[30];
extern char RxWiFiName[30];
extern char RxWiFiIP[30];
extern char RxCode[10];
extern char RxTemperature[30];
extern uint8_t RxFlag;

typedef struct
{
    uint8_t current;
    uint8_t previous;
    uint8_t next;
    uint8_t confirm;
    void (*function)(void);
} menu;

menu menulist[] =
    {
        {0, 1, 1, 0, (*NOP)},
        {1, 4, 2, 5, (*select1)},
        {2, 1, 3, 6, (*select2)},
        {3, 2, 4, 7, (*select3)},
        {4, 3, 1, 8, (*select4)},
        {5, 1, 1, 1, (*Weather_enter)},
        {6, 0, 0, 2, (*wallpaper)},
        {7, 1, 1, 3, (*NOP)},
        {8, 0, 0, 4, (*Internetenter)},

};

menu menu_wallpaper[] =
    {
        {0, 1, 1, 0, (*NOP)},
        {1, 2, 2, 2, (*wallpaper_bmp1)},
        {2, 1, 1, 2, (*wallpaper_bmp2)},
};

menu menu_Internet[] =
    {
        {0, 1, 1, 4, (*NOP)},
};

menu menu_Weather[] =
    {
        {0, 1, 1, 1, (*NOP)},
};

void RxPacket_clear(void)
{
    for (uint8_t i = 0; i < 30; i++)
    {
        RxPacket[i] = 0;
    }
}

void charstr(void)
{
    if (strstr(RxPacket, "connect WIFI success") == 0)
    {
        internet_flag = 1;
    }
    if (strstr(RxPacket, "Name") != NULL)
    {
        memcpy(RxWiFiName, RxPacket, sizeof(RxPacket));
    }
    if (strstr(RxPacket, "IP") != NULL)
    {
        memcpy(RxWiFiIP, RxPacket, sizeof(RxPacket));
        RxPacket_clear();
    }
    if (strstr(RxPacket, "Code") != NULL)
    {
        memcpy(RxCode, RxPacket, sizeof(RxPacket));
    }
    if (strstr(RxPacket, "Temperature") != NULL)
    {
        memcpy(RxTemperature, RxPacket, sizeof(RxPacket));
    }
}

void Contrl_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOC, &GPIO_InitStruct);
}

void Weather_Judgment(void)
{
    uint8_t range = 0;
    if (RxCode[6] == 0x20)
    {
        range = RxCode[5] - 0x30;
        if (range <= 3)
        {
            EPD_Dis_Part(32, 236, Sun, 64, 64);
            EPD_ShowString(4, 19, "Sun");
        }
        else if (range >= 4 && range <= 8)
        {
            EPD_Dis_Part(32, 236, PartlyCloudy, 64, 64);
            EPD_ShowString(4, 19, "PartlyCloudy");
        }
        else if (range == 9)
        {
            EPD_Dis_Part(32, 236, Cloudy, 64, 64);
            EPD_ShowString(4, 19, "Cloudy");
        }
        else if (range >= 10 && range <= 18)
        {
            EPD_Dis_Part(32, 236, Rain, 64, 64);
            EPD_ShowString(4, 19, "Rain");
        }
        else
        {
            EPD_ShowString(4, 19, "N/A");
        }
    }
    else
    {
        range = (RxCode[5] - 0x30) * 10 + RxCode[6] - 0x30;
        if (range <= 3)
        {
            EPD_Dis_Part(32, 236, Sun, 64, 64);
            EPD_ShowString(4, 19, "Sun");
        }
        else if (range >= 4 && range <= 8)
        {
            EPD_Dis_Part(32, 236, PartlyCloudy, 64, 64);
            EPD_ShowString(4, 19, "PartlyCloudy");
        }
        else if (range == 9)
        {
            EPD_Dis_Part(32, 236, Cloudy, 64, 64);
            EPD_ShowString(4, 19, "Cloudy");
        }
        else if (range >= 10 && range <= 18)
        {
            EPD_Dis_Part(32, 236, Rain, 64, 64);
            EPD_ShowString(4, 19, "Rain");
        }
        else
        {
            EPD_ShowString(4, 19, "N/A");
        }
    }
}

void NOP(void)
{
}

void select1(void)
{
    EPD_Dis_Part(102, 271, Select, 24, 8);          // select1
    EPD_Dis_Part(102, 197, SelectDisappear, 24, 8); // clear2
    EPD_Dis_Part(102, 123, SelectDisappear, 24, 8); // clear3
    EPD_Dis_Part(102, 49, SelectDisappear, 24, 8);  // clear4
}

void select2(void)
{
    EPD_Dis_Part(102, 197, Select, 24, 8);          // select2
    EPD_Dis_Part(102, 271, SelectDisappear, 24, 8); // clear1
    EPD_Dis_Part(102, 123, SelectDisappear, 24, 8); // clear3
    EPD_Dis_Part(102, 49, SelectDisappear, 24, 8);  // clear4
}

void select3(void)
{
    EPD_Dis_Part(102, 123, Select, 24, 8);          // select3
    EPD_Dis_Part(102, 197, SelectDisappear, 24, 8); // clear2
    EPD_Dis_Part(102, 271, SelectDisappear, 24, 8); // clear1
    EPD_Dis_Part(102, 49, SelectDisappear, 24, 8);  // clear4
}

void select4(void)
{
    EPD_Dis_Part(102, 49, Select, 24, 8);           // select4
    EPD_Dis_Part(102, 197, SelectDisappear, 24, 8); // clear2
    EPD_Dis_Part(102, 123, SelectDisappear, 24, 8); // clear3
    EPD_Dis_Part(102, 271, SelectDisappear, 24, 8); // clear1
}

void wallpaper_bmp1(void)
{
    EPD_WhiteScreen();
    Delay(450);
    EPD_WhiteScreen();
    EPD_ShowBMP(bmp);
}

void wallpaper_bmp2(void)
{
    EPD_WhiteScreen();
    Delay(450);
    EPD_WhiteScreen();
    EPD_ShowBMP(bmp2);
}

void wallpaper(void)
{
    EPD_WhiteScreen();
    EPD_ShowBMP(bmp);
    menu_wallpaper_operation();
}

void Internetenter(void)
{
    EPD_WhiteScreen();
    EPD_Dis_Part(16, 276, Internet_Information, 72, 16);
    if (internet_flag == 0)
    {
        EPD_Dis_Part(48, 276, Internet_FLag_OFF, 104, 16);
    }
    if (internet_flag == 1)
    {
        EPD_ShowString(4, 4, RxWiFiName);
        EPD_ShowString(5, 4, RxWiFiIP);
    }
    menu_internet_operation();
}

void Weather_enter(void)
{
    EPD_WhiteScreen();
    Weather_Judgment();
    EPD_ShowString(6, 19, RxTemperature);
    EPD_Dis_Part(80, 40, Cornermark, 8, 16);
    menu_Weather_operation();
}

void main_menu(void)
{
    EPD_Dis_Part(16, 291, Weather, 64, 64);
    EPD_Dis_Part(16, 217, Wallpaper, 64, 64);
    EPD_Dis_Part(16, 143, Clock, 64, 64);
    EPD_Dis_Part(16, 69, Internet, 64, 64);
    EPD_Dis_Part(80, 275, Chinese16x16tian, 16, 16);
    EPD_Dis_Part(80, 259, Chinese16x16qi, 16, 16);
    EPD_Dis_Part(80, 201, Chinese16x16bi, 16, 16);
    EPD_Dis_Part(80, 185, Chinese16x16zhi, 16, 16);
    EPD_Dis_Part(80, 127, Chinese16x16shi, 16, 16);
    EPD_Dis_Part(80, 111, Chinese16x16zhong, 16, 16);
    EPD_Dis_Part(80, 53, Chinese16x16wang, 16, 16);
    EPD_Dis_Part(80, 37, Chinese16x16luo, 16, 16);

    Delay(450);
    EPD_Dis_Part(16, 291, Weather, 64, 64);
    EPD_Dis_Part(16, 217, Wallpaper, 64, 64);
    EPD_Dis_Part(16, 143, Clock, 64, 64);
    EPD_Dis_Part(16, 69, Internet, 64, 64);
    EPD_Dis_Part(80, 275, Chinese16x16tian, 16, 16);
    EPD_Dis_Part(80, 259, Chinese16x16qi, 16, 16);
    EPD_Dis_Part(80, 201, Chinese16x16bi, 16, 16);
    EPD_Dis_Part(80, 185, Chinese16x16zhi, 16, 16);
    EPD_Dis_Part(80, 127, Chinese16x16shi, 16, 16);
    EPD_Dis_Part(80, 111, Chinese16x16zhong, 16, 16);
    EPD_Dis_Part(80, 53, Chinese16x16wang, 16, 16);
    EPD_Dis_Part(80, 37, Chinese16x16luo, 16, 16);
}

void menu_operation(void)
{
    // USART1_SendByte(0xff);
    while (1)
    {
        if (RxFlag == 1)
        {
            charstr();
            RxFlag = 0;
        }
        keynum = Key_GetNum();
        if (keynum == 1 && keyFlag == 0)
        {
            keyFlag = 1;
            menu_index = menulist[menu_index].previous;
            menulist[menu_index].function();
            keyFlag = 0;
        }
        if (keynum == 2 && keyFlag == 0)
        {
            keyFlag = 1;
            menu_index = menulist[menu_index].next;
            menulist[menu_index].function();
            keyFlag = 0;
        }
        if (keynum == 3 && keyFlag == 0)
        {
            menu_index = menulist[menu_index].confirm;
            menulist[menu_index].function();
        }
        OLED_ShowString(0, 10, RxCode, OLED_6X8);
        OLED_ShowString(0, 20, RxTemperature, OLED_6X8);
        OLED_ShowString(0, 30, RxWiFiIP, OLED_6X8);
        OLED_ShowString(0, 40, RxPacket, OLED_6X8);
        OLED_Update();
    }
}

void menu_wallpaper_operation(void)
{
    while (1)
    {
        keynum = Key_GetNum();
        if (keynum == 1 && keyFlag == 0)
        {
            keyFlag = 1;
            menu_wallpaper_index = menu_wallpaper[menu_wallpaper_index].previous;
            menu_wallpaper[menu_wallpaper_index].function();
            keyFlag = 0;
        }
        if (keynum == 2 && keyFlag == 0)
        {
            keyFlag = 1;
            menu_wallpaper_index = menu_wallpaper[menu_wallpaper_index].next;
            menu_wallpaper[menu_wallpaper_index].function();
            keyFlag = 0;
        }
        if (keynum == 3 && keyFlag == 0)
        {

            EPD_WhiteScreen();
            Delay(1700);
            EPD_WhiteScreen();
            Delay(1700);
            main_menu();
            menu_index = menu_wallpaper[menu_wallpaper_index].confirm;
            // menulist[menu_index].function();
            return;
        }
    }
}

void menu_internet_operation(void)
{
    while (1)
    {
        keynum = Key_GetNum();
        if (keynum == 1 && keyFlag == 0)
        {
            keyFlag = 1;
            keyFlag = 0;
        }
        if (keynum == 2 && keyFlag == 0)
        {
            keyFlag = 1;
            keyFlag = 0;
        }
        if (keynum == 3 && keyFlag == 0)
        {

            EPD_WhiteScreen();
            Delay(1700);
            EPD_WhiteScreen();
            Delay(1700);
            main_menu();
            menu_index = menu_Internet[0].confirm;
            return;
        }
    }
}

void menu_Weather_operation(void)
{
    while (1)
    {
        keynum = Key_GetNum();
        if (keynum == 1 && keyFlag == 0)
        {
            keyFlag = 1;
            keyFlag = 0;
        }
        if (keynum == 2 && keyFlag == 0)
        {
            keyFlag = 1;
            keyFlag = 0;
        }
        if (keynum == 3 && keyFlag == 0)
        {

            EPD_WhiteScreen();
            Delay(1700);
            EPD_WhiteScreen();
            Delay(1700);
            main_menu();
            menu_index = menu_Weather[0].confirm;
            return;
        }
    }
}
